<template>
    <div style="padding:20px;text-align: left;">
      <el-card class="kb-card" shadow="never">
        <div style="display: flex;justify-content: space-between;align-items: center;text-align: left;">
            <h1 class="title">设备列表</h1>
            <el-button type="primary" @click="drawerVisible = true"><el-icon>
                    <Plus />
                </el-icon>创建设备</el-button>
        </div>
        <div>
            <div v-if="deviceList?.length == 0">
                <el-empty description="暂无设备"></el-empty>
            </div>
            <el-row :gutter="12" v-else>
                <el-col :span="6" v-for="device in deviceList" :key="device.id">
                    <div style="margin:4px;background-color: #fff;">
                        <div>
                            <img src="../../assets/deviceBanner.webp" style="width: 100%;height: auto;" />
                        </div>
                        <div style="padding:8px">
                            <div>{{ device.name }} ，来自于{{ device.location }}</div>
                            <div></div>
                            <!-- <div>来源：</div> -->
                            <div style="color:#999"><small>最后使用时间：{{ device.login_time }}</small></div>
                            <div style="color:#999"><small>Mac：{{ device.mac }}</small></div>
                            <div style="text-align: right;">
                                <el-button size="small" type="primary"
                                    @click="() => { this.$router.push(`/deviceInfo/${device.id}`) }">查看</el-button>
                                <el-button size="small" type="danger" @click="deleteDevice(device)">删除</el-button>
                            </div>
                        </div>
                    </div>
                </el-col>
            </el-row>
        </div>
        </el-card>
    </div>

    <el-drawer v-model="drawerVisible" title="新增设备" :direction="direction" :before-close="handleClose" style="text-align: left;">
        <el-form label-position="top">
            <el-form-item label="设备名称">
                <el-input placeholder="请输入设备名称。" v-model="form.name" />
            </el-form-item>
            <el-form-item label="设备介绍">
                <el-input placeholder="请输入设备介绍。" v-model="form.introduction" />
            </el-form-item>
            <el-form-item label="设备Mac">
                <el-input placeholder="请输入设备Mac，位于设备说明书的Mac标签中。" v-model="form.mac" />
            </el-form-item>
        </el-form>

        <template #footer>
            <div style="flex: auto">
                <el-button @click="drawerVisible = false">取消</el-button>
                <el-button type="primary" @click="createDevice">确认</el-button>
            </div>
        </template>
    </el-drawer>
</template>

<script>
export default {
    data() {
        return {
            deviceList: [],
            drawerVisible: false,
            direction: 'rtl',
            form: { device_id: "", name: "", introduction: "", location: "", id: 0 },
        }
    },
    methods: {
        async getDeviceList() {
            try {
                const response = await fetch(`${import.meta.env.VITE_API_URL}/deviceBank/getDeviceList`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({})
                });
        if (response.status === 401) {
          localStorage.removeItem('access_token');
          this.$router.push('/login');
          return;
        }
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const rs = await response.json();
                console.log("rs:>>:", rs);
                this.deviceList = rs.data;
            } catch (error) {
                console.error('There was a problem with the fetch operation:', error);
            }
        },
        async createDevice() {
            try {
                const response = await fetch(`${import.meta.env.VITE_API_URL}/deviceBank/createDevice`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(this.form)
                });
        if (response.status === 401) {
          localStorage.removeItem('access_token');
          this.$router.push('/login');
          return;
        }
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const rs = await response.json();
                console.log("rs:>>:", rs);
                this.drawerVisible = false;
                this.getDeviceList();
            } catch (error) {
                console.error('There was a problem with the fetch operation:', error);
            }
        },
        async deleteDevice(row) {
            try {
                const response = await fetch(`${import.meta.env.VITE_API_URL}/deviceBank/deleteDevice`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(row)
                });
        if (response.status === 401) {
          localStorage.removeItem('access_token');
          this.$router.push('/login');
          return;
        }
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                const rs = await response.json();
                console.log("rs:>>:", rs);
                this.getDeviceList();
            } catch (error) {
                console.error('There was a problem with the fetch operation:', error);
            }
        },
        handleClose(done) {
            this.$confirm('确定要关闭吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                done();
            }).catch(() => {});
        }
    },
    mounted() {
        this.getDeviceList();
    }
}
</script>

<style></style>



